package org.apache.myfaces.blank;

import java.util.List;

import javax.faces.model.DataModel;

public class PagedListDataModel extends DataModel<Amigo> {

	private int rowIndex = -1;

	private int totalNumRows;

	private int pageSize;

	private List<Amigo> list;

	public PagedListDataModel() {
		super();
		list.add(new Amigo("CACHILO",3.0));
		list.add(new Amigo("CACHILA",3.0));
		list.add(new Amigo("CACHILE",3.0));
		list.add(new Amigo("CACHILQ",3.0));
		list.add(new Amigo("CACHILA",3.0));
		list.add(new Amigo("CACHIL4",3.0));
		list.add(new Amigo("CACHIL5",3.0));
		list.add(new Amigo("CACHIL6",3.0));
		list.add(new Amigo("CACHIL7",3.0));
		list.add(new Amigo("CACHIL8",3.0));
	}

	public PagedListDataModel(List<Amigo> list, int totalNumRows, int pageSize) {
		super();
		setWrappedData(list);
		this.totalNumRows = totalNumRows;
		this.pageSize = pageSize;
	}

	@Override
	public boolean isRowAvailable() {
		if (list == null)
			return false;

		int rowIndex = getRowIndex();
		if (rowIndex >= 0 && rowIndex < list.size())
			return true;
		else
			return false;
	}

	public int getRowCount() {
		return totalNumRows;
	}

	public Amigo getRowData() {
		if (list == null)
			return null;
		else if (!isRowAvailable())
			throw new IllegalArgumentException();
		else {
			int dataIndex = getRowIndex();
			return list.get(dataIndex);
		}
	}

	public int getRowIndex() {
		return (rowIndex % pageSize);
	}

	public void setRowIndex(int rowIndex) {
		this.rowIndex = rowIndex;
	}

	public Object getWrappedData() {
		return list;
	}

	public void setWrappedData(Object list) {
		this.list = (List<Amigo>) list;
	}

	public List<Amigo> getPagedData(int start, int page) {
		return this.list.subList(start, pageSize * page);
	}

	public int getDataCount() {
		return list.size();
	}

	
}
